#!/bin/sh
#desc: Crea una ISO con todos los archivos necesarios para arrancar una MV

# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later

# ----------------------------------------------------------------------------
#  Script Configuration
# ----------------------------------------------------------------------------
KERNEL=../../buildroot/output/images/bzImage
ROOTFS=bootcd/initrd.gz
ISOFILES=../../isofiles
KEYMAPS=../../keymaps
WORK=/tmp
MNTDIR=/mnt/opendomo
INITRDSIZE=32

if test -z "$1"
then
	ISONAME="opendomo-unlabeled-486.iso"
else
	ODVER="$1"
	echo "----------------------------------------------"
	echo " Packaging release $ODVER!"
	echo "----------------------------------------------"
	sleep 1
	ISONAME="opendomo-$ODVER-486.iso"
fi

# If it does not exist, create
mkdir -p bootcd

# ----------------------------------------------------------------------------
#  Distro Files
# ----------------------------------------------------------------------------
# Verify all file existence before continuing
if test -f $KERNEL
then
	cp $KERNEL bootcd/linux
else
	echo "ERROR: $KERNEL not found !!"
	exit 1
fi
if ! test -f "$ROOTFS"
then
	echo "ERROR: $ROOTFS not found!!"
	exit 1
fi
if ! test -d $ISOFILES
then
	echo "ERROR: $ISOFILES not found!!"
	wget www.opendomo.org/files/isofiles.tar.gz
	tar -zxvf isofiles.tar.gz
	mv isofiles $ISOFILES
fi
cp -fr $ISOFILES/* bootcd/
# Copy also isofiles from SVN (if they exist!)
cp isofiles/* bootcd/

# -- Information --
echo "Generating OpenDomo ISO file with following data:
   Kernel used: $KERNEL
   Initrd:      $ROOTFS
   Mount dir:   $MNTDIR"

#chmod 666 packages/*

ODCGI=`ls packages/odcgi*.tar.gz | head -n 1` 2>/dev/null
ODDISCOVERY=`ls packages/oddiscovery*.tar.gz | head -n 1` 2>/dev/null
ODCOMMON=`ls packages/odcommon*.tar.gz | head -n 1` 2> /dev/null
ODHAL=`ls packages/odhal*.tar.gz | head -n 1` 2> /dev/null
ODPKG=`ls packages/odpkg*.tar.gz | head -n 1` 2> /dev/null

if ! test -f "$ODCGI"; then
	echo "WARNING: odcgi not found. Fetching..."
	cp ../opendomo-cgi/pkg/odcgi*.tar.gz packages/ || exit 1
fi
if ! test -f "$ODDISCOVERY"; then
	echo "WARNING: oddiscovery not found. Fetching..."
	cp ../opendomo-discovery/pkg/oddiscovery*.tar.gz packages/ || exit 1
fi
if ! test -f "$ODCOMMON"; then
	echo "WARNING: odcommon not found. Fetching..."
	cp ../opendomo-common/pkg/odcommon*.tar.gz packages/ || exit 1
fi
if ! test -f "$ODHAL"; then
	echo "WARNING: odhal not found. Fetching..."
	cp ../opendomo-hal/pkg/odhal*.tar.gz packages/ || exit 1
fi
if ! test -f "$ODPKG"; then
	echo "WARNING: odpkg not found. Fetching..."
	cp ../opendomo-pkg/pkg/odpkg*.tar.gz packages/ || exit 1
fi

if test `ls packages/*.gz | wc -l` -ne `ls packages/*.gz | cut -f 1 -d'-' | uniq | wc -l`; then
	echo "ERROR: Repeated package name"
	exit 1
fi
echo


# With this we keep the CD mounted in /mnt/opendomodistro after booting
echo 'LABEL="opendomodistro"' > bootcd/opendomo.cfg
echo 'CONFDEVICE="1"' >> bootcd/opendomo.cfg
echo 'SYSDEVICE="1"' >> bootcd/opendomo.cfg

# Data files
mkdir -p bootcd/data
cp data/* bootcd/data/

# Package cache
mkdir -p bootcd/pkgcache
rm -fr bootcd/pkgcache/*
cp packages/*.tar.gz bootcd/pkgcache/

cd bootcd/pkgcache
echo "Packages included:"
for i in *.tar.gz  
do
	BN=`basename $i | sed 's/\.tar\.gz//'`
	echo "   $BN"
done

cd ../..


USR="--owner 1000 --group 1000 "
EXCLUDE=" --exclude '*~' --exclude .svn "
cd sysconf
if ! test -z "$ODVER"; then
	touch etc/opendomo/repositories.cfg
	chown 1000 etc/opendomo/repositories.cfg
	echo "http://es.opendomo.org/files/pkg/" > etc/opendomo/repositories.cfg
#	echo "http://es.opendomo.org/files/pkg/$ODVER/" >> etc/opendomo/repositories.cfg
	echo $ODVER > ../bootcd/VERSION
fi
mkdir -p ../bootcd/sysconf 
tar -zcf ../bootcd/sysconf/sysconf.tar.gz * $USR $EXCLUDE
cd ..


# ----------------------------------------------------------------------------
#  Make ISO
# ----------------------------------------------------------------------------
if test -x /usr/bin/mkisofs ; then
	MKISO="/usr/bin/mkisofs"
else
	if test -x /usr/bin/genisoimage; then
		MKISO="/usr/bin/genisoimage"
	else
		echo "#ERROR: mkisofs or genisoimage not found"
		exit 1
	fi
fi


if $MKISO -o ../$ISONAME -b isolinux.bin -c boot.cat -no-emul-boot \
	-boot-load-size 4 -boot-info-table -J -input-charset utf-8 \
	-hide-rr-moved -R bootcd/ 2>/dev/null
then
	echo "Finished:"
	ls -lah ../$ISONAME
fi


